//index.js
//获取应用实例
const app = getApp()
import {
    getInfo
} from '../../api/mch'
import {
    getTokenByCode,
    register
} from '../../api/user'
import {
    lockSysUser
} from '../../api/member'
const util = require('../../utils/util.js')

Page({
    data: {
        motto: '芸店提供技术支持',
        mch: {},
        cardType: false,
        code:"",
        SHARE_TYPE:0,
        xuid:null
    },
    onLoad: function (option) {
        var _this = this;
        this.data.SHARE_TYPE=app.SHARE_TYPE
        this.data.xuid=app.xuid
        this.setData({
          SHARE_TYPE:this.data.SHARE_TYPE,
          xuid:this.data.xuid
        })
        // 获取商户信息
        console.log(option)
        if(option.cardType != undefined){
            _this.data.cardType = option.cardType
        }
        getInfo(app.globalData.mchId).then(
            res => {
                console.log(res)
                if (res.type == 'success') {
                    _this.data.mch = res.data
                    _this.setData({
                        mch: _this.data.mch
                    })
                }
            }
        ).catch(error => {
            console.log(error)
        })

    },
    startLockSysUser() {
        console.log('查看登录后的xuid')
        console.log(app.globalData.xuid)
        lockSysUser({ mchId: app.globalData.mchId, xuid: app.globalData.xuid, shopId: app.globalData.shopId }).then(res => {
            console.log(res)
        })
    },
    inputcodeNum(e){
        this.data.code = e.detail.value
        this.setData({
          code: this.data.code
        })
      } ,
    callRegister: function (e,userId) {
        var _this = this

        let mchId = app.globalData.mchId;
        if (app.APP_TYPE > 0) {
            mchId = app.ISV_ID
        }
        register({
            mchId: mchId,
            uuid: app.globalData.uuid,
            encryptData: e.detail.encryptedData,
            iv: e.detail.iv,
            code: this.data.code
        }).then(
            res => {
                console.log(res)
                if (res.type == 'success') {
                    const {
                        userInfo,
                        token
                    } = res.data
                    console.info("res.data--" + JSON.stringify(res.data))
                    console.info("token--" + JSON.stringify(token))

                    app.globalData.token = token
                    console.info("app.globalData.token--" + JSON.stringify(app.globalData.token))
                    app.globalData.userInfo = userInfo
                    app.globalData.uuid = userId
                    _this.startLockSysUser();
                    wx.navigateBack({
                        delta: 1
                    })
                    const eventChannel = _this.getOpenerEventChannel()
                    eventChannel.emit('acceptDataFromOpenedPage', {
                        cardType: _this.data.cardType
                    });
                } else {
                    wx.showToast({
                        icon: "none",
                        title: res.content
                    })
                }
            }
        ).catch(error => {
            console.log(error)
        })
    },
    getUserInfo: function (e) {
        var _this = this;
        let mchId = app.globalData.mchId;
        if (app.APP_TYPE > 0) {
            mchId = app.ISV_ID
        }
        wx.login({
            success: wxres => {
                console.log(wxres)
                getTokenByCode({
                    mchId: mchId,
                    auth_code: wxres.code,
                    uuid: app.globalData.uuid,
                    pluginId: 'weixinMiniAuthPlugin'
                }).then(
                    res => {
                        console.log(res)
                        if (res.type == 'success') {
                            _this.callRegister(e,res.data.userId)
                        } else {
                            wx.showToast({
                                icon: "none",
                                title: res.content
                            })
                        }
                    }
                ).catch(error => {
                    console.log(error)
                })
            }
        })
    }
})
